﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChuanYe.CountyFireManagement.Api.Models;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ChuanYe.CountyFireManagement.Api.Controllers
{
    /// <summary>
    /// 成立前指
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FireFrontCommandController : ControllerBase
    {

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="obj">obj.FireId</param>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Add(FireFrontCommandRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                string ip = HttpContext.GetClientIp();
                Firefrontcommand info = new Firefrontcommand();
                info.FireId = obj.FireId;
                info.CommanderTel = obj.CommanderTel;
                info.CommanderName = obj.CommanderName;
                info.HeadquarterMemberIds = obj.HeadquarterMemberIds;
                info.Latitude = obj.Latitude;
                info.Longitude=obj.Longitude;
                info.TransDatetime = obj.TransDatetime.HasValue ? obj.TransDatetime.Value : DateTime.Now;
                info.Ip = ip;
                info.UserId = obj.UserId;
                
                var data = BLL.FireFrontCommandBLL.Instance.Add(info);
                if (data.FirstParam != null)
                {
                    ControllerHelper.SendMessage(data.FirstParam);
                    var allExpireFreInfo = BLL.FireExpireBLL.Instance.GetUnExpire();
                    MqttHub.fireexpires = allExpireFreInfo;
                }
                result.Status = data.Status;
                result.Message = data.Message;
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/FireFrontCommand/Add Server Error";
                LogHelper.Error(result.Message,ex);
            }
            return result;
        }




    }
}